package com.yuanluludev.lib_base.base

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.yuanluludev.lib_base.R

/**
 * 不带网络的弹窗
 * yuanLuLu
 */
abstract class BaseDialogFragment : DialogFragment() {

    /**
     * 是否需要使用DataBinding 供子类修改，用户请慎动
     */
    private var isUseDB = false
    protected var mContext: Context? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        dialog?.apply {
            // 隐藏标题栏, 不加弹窗上方会一个透明的标题栏占着空间
            requestWindowFeature(Window.FEATURE_NO_TITLE)
            // 必须设置这两个,才能设置宽度
            window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            window?.decorView?.setBackgroundColor(Color.TRANSPARENT)
            // 遮罩层透明度
            window?.setDimAmount(0.5f)
            // 设置宽高
            window?.attributes?.apply {
                width = getDialogWidth()
                height = getDialogHeight()
                gravity = Gravity.CENTER

                dialog?.window?.attributes = this
            }
        }

        return if (!isUseDB) {
            inflater.inflate(getLayoutResId(), container, false)
        } else {
            initViewModel()
            //with view data binding
            if (this is BaseVMWithDBDialogFragment<*, *>) {
                initDataBinding(inflater, container)?.root
            } else {
                inflater.inflate(getLayoutResId(), container, false)
            }
        }

    }

    /**
     * 让子类初始化ViewModel
     */
    open fun initViewModel() {}

    /**
     * 让子类初始化DataBinding
     */
    open fun initDataBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): ViewDataBinding? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        dealCommonEvent(view)

        initEvents()

    }

    open fun initEvents() {}

    private fun dealCommonEvent(view: View) {
        (view.findViewById<View?>(R.id.dismissDialog))?.setOnClickListener {
            dismiss()
        }
    }

    fun useDataBinding(isUseDB: Boolean) {
        this.isUseDB = isUseDB
    }

    //默认宽度为屏幕的0.8倍（子类可重写）
    open fun getDialogWidth() = ViewGroup.LayoutParams.WRAP_CONTENT

    //默认高度为自适应（子类可重写）
    open fun getDialogHeight() = ViewGroup.LayoutParams.WRAP_CONTENT

    abstract fun getLayoutResId(): Int

    fun show(
        fragmentManager: FragmentManager?,
        isCancelable: Boolean = true
    ) {
        this.isCancelable = isCancelable
        try {
            fragmentManager?.let {
                show(it, this.javaClass.simpleName)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    open fun showProgress(message: String = "正在加载...") =
        (mContext as BaseActivity?)?.showLoading(message, isCancelable)

    open fun hideProgress() = (mContext as BaseActivity?)?.hideLoading()

}